{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "numpy是python进行数据分析重要的库\n",
    "\"\"\"\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<type 'numpy.ndarray'>\n",
      "True\n",
      "(2L, 3L)\n",
      "int32\n",
      "True\n",
      "\u0001\u0000\u0000\u0000\u0002\u0000\u0000\u0000\u0003\u0000\u0000\u0000\u0004\u0000\u0000\u0000\u0005\u0000\u0000\u0000\u0006\u0000\u0000\u0000\n",
      "2\n",
      "None\n",
      "6\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "<numpy.flatiter object at 0x000000000303FCE0>\n"
     ]
    }
   ],
   "source": [
    "# ndarray的属性\n",
    "# ----\n",
    "# numpy中一个最重要的对象格式ndarray，即n维数组，要求所有元素的类型必须一致\n",
    "arr = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(type(arr))\n",
    "print(type(arr) == np.ndarray)\n",
    "\n",
    "# numpy作为数组有三个重要属性：shape和dtype,data\n",
    "print(arr.shape)\n",
    "print(arr.dtype)\n",
    "print(arr.dtype==np.int32)\n",
    "print(arr.data)\n",
    "\n",
    "# 除此之外还有'维度'、'基础数组'(如果该数组是某个数组的一个视图则该数组的基础数组是原数组)、‘长度’、'实数部分'\n",
    "print(arr.ndim)\n",
    "print(arr.base)\n",
    "print(arr.size)\n",
    "print(arr.real)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2L, 3L)\n",
      "(2L,)\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "invalid literal for long() with base 10: 'a'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-33-e468a152161a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[1;31m# 索引\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0marr\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"a\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     10\u001b[0m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marr\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for long() with base 10: 'a'"
     ]
    }
   ],
   "source": [
    "# ndarray的方法\n",
    "# 生成\n",
    "# array用于将可迭代对象生成数组, 这是一个很智能的函数, 如果外层的可迭代对象的size不一致它就不会将其作为一个维度了\n",
    "arr = np.array([(1, 2, 3), (4, 5, 6)])\n",
    "print(arr.shape)\n",
    "print(np.array([(1, 2, 3), (4, 5)]).shape)\n",
    "\n",
    "# 索引\n",
    "print(arr[1][0])\n",
    "# 修改变量\n",
    "arr[1][0] = 6\n",
    "print(arr[1][0])\n",
    "# "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
